
package scm.inventoryControl.managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import scm.inventoryControl.entity.ProductBatchRecordEntity;
import scm.inventoryControl.entity.RMInventoryLevelEntity;
import scm.inventoryControl.entity.RawMaterialBatchRecordEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
import scm.rawMaterialPurchasing.session.BOMSessionBean;


@ManagedBean
@ViewScoped
public class CreateRMRecordManagedBean implements Serializable  {
    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    @EJB
    private BOMSessionBean bomSessionBean;
    private List<RawMaterialBatchRecordEntity> rmRecordList;
    private String rawMaterialName;
    private RawMaterialBatchRecordEntity rawMaterialBatchRecord;
    private RMInventoryLevelEntity rmInventoryLevel;
    
    public CreateRMRecordManagedBean() {
          rmRecordList = new ArrayList<RawMaterialBatchRecordEntity>();
          rawMaterialBatchRecord=new RawMaterialBatchRecordEntity();
    }
    
    public List<String> getRawMaterialNames(){
          return bomSessionBean.getAllRawMaterialNames();
    }
    
    public void deleteRMRecord(RawMaterialBatchRecordEntity rmBatchRecord) {
        for (int i = 0; i < rmRecordList.size(); i++) {
            if (rmRecordList.get(i).getRmInventoryLevel().getRawMaterial().getRawMaterialName().equals(rmBatchRecord.getRmInventoryLevel().getRawMaterial().getRawMaterialName())) {
                rmRecordList.remove(i);
                break;
            }
        }
    }
    
     public void addRWRecord(ActionEvent event) throws IOException {
        rmInventoryLevel = inventorySessionBean.getInventoryByRMName(rawMaterialName);
        rawMaterialBatchRecord.setRmInventoryLevel(rmInventoryLevel);
        rmRecordList.add(rawMaterialBatchRecord);
        rawMaterialBatchRecord = new RawMaterialBatchRecordEntity();
    }
     
    
     public void submitRecord(ActionEvent event) throws IOException {//save record &  update inventory level
        for (int i = 0; i < rmRecordList.size(); i++) {
            inventorySessionBean.saveRMRecord(rmRecordList.get(i));
             inventorySessionBean.adjustRMInventoryLevel(rmRecordList.get(i));
        }
        //connected inventoryLevel to productRecords
       // inventorySessionBean.connectInventoryToRecords(getProductRecordList());
        rmRecordList.clear();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Records has been successfully saved.", ""));
    }

     

    public List<RawMaterialBatchRecordEntity> getRmRecordList() {
        return rmRecordList;
    }

    public void setRmRecordList(List<RawMaterialBatchRecordEntity> rmRecordList) {
        this.rmRecordList = rmRecordList;
    }

    public String getRawMaterialName() {
        return rawMaterialName;
    }

    public void setRawMaterialName(String rawMaterialName) {
        this.rawMaterialName = rawMaterialName;
    }

    public RawMaterialBatchRecordEntity getRawMaterialBatchRecord() {
        return rawMaterialBatchRecord;
    }

    public void setRawMaterialBatchRecord(RawMaterialBatchRecordEntity rawMaterialBatchRecord) {
        this.rawMaterialBatchRecord = rawMaterialBatchRecord;
    }

    public RMInventoryLevelEntity getRmInventoryLevel() {
        return rmInventoryLevel;
    }

    public void setRmInventoryLevel(RMInventoryLevelEntity rmInventoryLevel) {
        this.rmInventoryLevel = rmInventoryLevel;
    }
    
}
